//
//  ViewController.m
//  YHtableViewCellHeightDemo
//
//  Created by apple on 17/4/15.
//  Copyright © 2017年 wax. All rights reserved.
//

#import "ViewController.h"

//#import "TestTableViewCell.h"

#import "ImageTableViewCell.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,weak)UITableView *tableView;

@end

@implementation ViewController{

    //数据源数组
    NSArray *_datasArray;
    
}

static NSString *cellID = @"ImageTableViewCell";
//static NSString *cellID = @"TestTableViewCell";


- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor orangeColor];
    
    //1.创建tableView
    [self prepareTableView];
    
    //2.模拟网络请求 - 图片数据
    [self getDatas2];
  
}

#pragma mark - 创建基础控件

-(void)prepareTableView{

    UITableView *tableView =[[UITableView alloc]init];
    
    tableView.frame = self.view.bounds;
    
    [self.view addSubview:tableView];
    
    tableView.dataSource = self;
    
    tableView.delegate = self;
    
    _tableView = tableView;
    
    //cell高度
    _tableView.estimatedRowHeight = 44;
    _tableView.rowHeight = UITableViewAutomaticDimension;

}

#pragma mark - 模拟网络请求

//2.仅有图片
-(void)getDatas2{
    
    NSMutableArray *arrayM = [NSMutableArray array];
    
    //暂定图片三张
    
    for (int i = 0; i<20;i++) {
        
        NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
        
        if (i % 5 ==0) {
            
            [dictM setObject:[NSString stringWithFormat:@"短key-%d",i] forKey:@"myKey"];
            
            NSInteger tempI = i;
            
            tempI = tempI % 9;

            [dictM setObject:[NSString stringWithFormat:@"%ld",(long)tempI] forKey:@"myValue"];
            
            [arrayM addObject:dictM];
            
            continue;
        }
        else if (i % 4 == 0){
            
            [dictM setObject:[NSString stringWithFormat:@"长长长长key-%d",i] forKey:@"myKey"];
            
            NSInteger tempI = i;
            
            tempI = tempI % 9;
            
            [dictM setObject:[NSString stringWithFormat:@"%ld",(long)tempI] forKey:@"myValue"];
            [arrayM addObject:dictM];
            continue;
        }
        else if(i % 7 ==0){
            
            [dictM setObject:[NSString stringWithFormat:@"mini-%d",i] forKey:@"myKey"];
            
            NSInteger tempI = i;
            
            tempI = tempI % 9;
            
            [dictM setObject:[NSString stringWithFormat:@"%ld",(long)tempI] forKey:@"myValue"];
            
            [arrayM addObject:dictM];
            continue;
        }
        else{
            
            [dictM setObject:[NSString stringWithFormat:@"正常的-%d",i] forKey:@"myKey"];
            
            NSInteger tempI = i;
            
            tempI = tempI % 9;
            
            [dictM setObject:[NSString stringWithFormat:@"%ld",(long)tempI] forKey:@"myValue"];
            
            [arrayM addObject:dictM];
        }
        
        
    }
    NSLog(@"数据请求完成->数据源arrayM = %@ ",arrayM);
    
    _datasArray = arrayM.copy;
    
}

//1.仅有文字
-(void)getDatas1{
    
    NSMutableArray *arrayM = [NSMutableArray array];

    //暂定图片三张
    
    for (int i = 0; i<20;i++) {
        
        NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
        
        if (i % 5 ==0) {
            
            [dictM setObject:[NSString stringWithFormat:@"短key-%d",i] forKey:@"myKey"];
            
            [dictM setObject:[NSString stringWithFormat:@"我只有一行 + %d",i] forKey:@"myValue"];
            
            [arrayM addObject:dictM];
            
            continue;
        }
        else if (i % 4 == 0){
            
            [dictM setObject:[NSString stringWithFormat:@"长长长长key-%d",i] forKey:@"myKey"];
            
            [dictM setObject:[NSString stringWithFormat:@"名字很长的我-------------------------------------够长了吧！！ + %d",i] forKey:@"myValue"];
            
            [arrayM addObject:dictM];
            continue;
        }
        else if(i % 7 ==0){
            
            [dictM setObject:[NSString stringWithFormat:@"mini-%d",i] forKey:@"myKey"];
            
            [dictM setObject:[NSString stringWithFormat:@"不多说了兄弟，我最长了吧-->HAHA-------------------------------------够长了吧！！ + %d",i] forKey:@"myValue"];
            
            [arrayM addObject:dictM];
            continue;
        }
        else{
            
            [dictM setObject:[NSString stringWithFormat:@"正常的-%d",i] forKey:@"myKey"];
            
            [dictM setObject:@"短短的" forKey:@"myValue"];
            
            [arrayM addObject:dictM];
        }
        
        
    }
    NSLog(@"数据请求完成->数据源arrayM = %@ ",arrayM);
    
    _datasArray = arrayM.copy;

}


#pragma mark - <tableViewDelegate>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _datasArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        
        cell = [ImageTableViewCell loadCell];
    }
    
    //取消cell点击选择的效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //设置数据源
    NSDictionary *dict = _datasArray[indexPath.row];
    
    cell.dataDic = dict;
    
    return cell;
}

//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//
//    return 100;
//    
//}

@end
